package teach.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import teach.aistar.model.entity.CarBrands;
import teach.aistar.model.entity.CarDetail;
import teach.aistar.model.entity.CarStyle;
import teach.aistar.model.entity.UserDetail;
import teach.aistar.service.ICarBandsService;
import teach.aistar.service.ICarDetailService;
import teach.aistar.service.ICarStyleService;
import teach.aistar.service.IUserDetailService;
import teach.aistar.util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class SellcarController {
    @Autowired
    private ICarDetailService iCarDetailService;

    @Autowired
    private ICarBandsService iCarBandsService;

    @Autowired
    private ICarStyleService iCarStyleService;

    @Autowired
    private IUserDetailService iUserDetailService;


    @GetMapping("/saleCar")
    public String bands(HttpServletRequest request, HttpSession session) {
        List<CarBrands> bands = iCarBandsService.selectAll();
        System.out.println(bands);
        List<CarStyle> styles = iCarStyleService.findAll();
        List<UserDetail> userDetails =iUserDetailService.selectDetailAll();
        request.setAttribute("brandsList", bands);
        request.setAttribute("styleList", styles);
        request.setAttribute("userDetailsList",userDetails);
        return "sell-car";
    }


    @GetMapping("/sell-car")
    public String add(HttpServletRequest request, HttpSession session) throws ParseException {
        String carBrands = (String) request.getParameter("carBrands");
        String carstyle = (String) request.getParameter("carstyle");
        String seatnum = (String) request.getParameter("seatnum");
        String speed = (String) request.getParameter("speed");
        String price = (String) request.getParameter("price");
        String mile = (String) request.getParameter("mile");
        String time = (String) request.getParameter("time");
        String startime = (String) request.getParameter("startime");
        String carPicture = (String) request.getParameter("carPicture");
        String descript = (String) request.getParameter("descript");

        String address=request.getParameter("address");
        String uphone=request.getParameter("uphone");
        String email=request.getParameter("email");
        String uname=request.getParameter("uname");



        System.out.println("地址："+address);
        System.out.println("地址："+uphone);
        System.out.println("地址："+email);
        System.out.println("地址："+uname);
        System.out.println("价格"+price);

        UserDetail userDetail=new UserDetail();
        if (address!=null){
            userDetail.setAddress(address);
        }
        if (uphone!=null){
            userDetail.setUphone(uphone);
        }
        if (email!=null){
            userDetail.setEmail(email);
        }
        if (uname!=null){
            userDetail.setUname(uname);
        }
        iUserDetailService.insert(userDetail);

        CarDetail carDetail = new CarDetail();
        if (carBrands != null) {
            carDetail.setBrId(Long.valueOf(carBrands));
        }
        if (carstyle != null) {
            carDetail.setStId(Long.valueOf(carstyle));
        }
        if (price != null) {
            carDetail.setcOprice(Double.valueOf(price));
        }
        if (seatnum != null) {
            carDetail.setcSeat(Integer.valueOf(seatnum));
        }
        if (mile != null) {
            carDetail.setcMileage(Integer.valueOf(mile));
        }
        if (speed != null) {
            carDetail.setcGear(speed);
        }
        if (time != null) {
            carDetail.setcInsure(DateUtil.stringToDate(time));
        }
        if (startime != null) {
            carDetail.setcRegistration(DateUtil.stringToDate(startime));
        }
        //处理车的图片路径
//        if (!carPicture.equals("")) {
//            String[] carPictures = carPicture.split("/");
//            carDetail.setcPhoto(carPictures[carPictures.length - 1]);
//        }
        if (!descript.equals("")) {
            carDetail.setcDesc(descript);
        }
        iCarDetailService.insert(carDetail);
        return "sell-carok";
    }

//    @GetMapping("/sell-cars")
//    public String adds(HttpServletRequest request,HttpSession session) throws ParseException{
//        String address=request.getParameter("address");
//        String phone=request.getParameter("phone");
//        String email=request.getParameter("email");
//        String uname=request.getParameter("uname");
//        UserDetail userDetail=new UserDetail();
//        if (address!=null){
//            userDetail.setAddress(address);
//        }
//        if (phone!=null){
//            userDetail.setIdCard(phone);
//        }
//        if (email!=null){
//            userDetail.setEmail(email);
//        }
//        if (uname!=null){
//            userDetail.setUname(uname);
//        }
//        iUserDetailService.insert(userDetail);
//        return "index";
//    }
}

